<?php


/**
 * Notes:
 * PROJECT_NAME : ERP v1.8 重构erp
 * User: 夏鱼儿<397072174@qq.com>
 * Date: 2024/3/25
 * Time: 10:33
 */

namespace app\service\base;

use app\BaseController;
use app\service\traits\JsonTrait;
use Exception;

class Controller extends BaseController
{

    protected string $validateClass    = '';//验证类
    protected string $withSearch       = '';//查询时需要检索的字段
    protected array  $checkUniqueField = [];//修改时校验唯一字段
    protected string $field            = '';//查询字段约束
    protected string $append           = '';//查询时追加的字段
    protected string $with             = "";//关联表
    protected string $hidden           = "";//查询时需要隐藏的字段
    protected array  $orderSort        = ['id' => 'desc'];//排序
    protected string $detailWith       = "";//明细需要关联的表

    protected int    $company_id  = 0;//公司id
    protected int    $supplier_id = 0;//第三方id
    protected mixed  $userInfo;//用户信息
    protected int    $user_id     = 0;//用户id
    public bool      $isGs        = false; //是否验证公司ID
    protected string $appid       = '';//小程序id
    //是否查询已删除
    protected bool $isDel = false;

    use JsonTrait;

    /**
     * 验证字段唯一
     * @param $model
     * @param $fields
     * @param $param
     * @param int $id
     * @return void
     * @throws Exception
     */
    public function uniqueField($model, $fields, $param, int $id = 0): void
    {
        foreach ($fields as $field) {
            if (isset($param[$field])) {
                if ($id) $model = $model->whereNotIn('id', $id);
                if ($model->where($field, $param[$field])->find()) exception($field . '已存在');
            }
        }
    }


}